﻿using System;
using System.Collections.Generic;
using Infrastructure;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository.Domain;

namespace OpenAuth.WebApi.Controllers
{
    /// <summary>
    /// InspectPlan操作
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InspectPlansController : ControllerBase
    {
        private readonly InspectPlanApp _app;

        //获取详情
        [HttpGet]
        public Response<InspectPlan> Get(string id)
        {
            var result = new Response<InspectPlan>();
            try
            {
                result.Result = _app.GetOne(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //添加
        [HttpPost]
        public Response Add(InspectPlan obj)
        {
            var result = new Response();
            try
            {
                _app.Add(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        //修改
        [HttpPost]
        public Response Update(InspectPlan obj)
        {
            var result = new Response();
            try
            {
                _app.Update(obj);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        /// <summary>
        /// 加载列表
        /// </summary>
        [HttpGet]
        public TableData Load([FromQuery]QueryInspectPlanListReq request)
        {
            return _app.Load(request);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        [HttpPost]
        public Response Delete([FromBody]string[] ids)
        {
            var result = new Response();
            try
            {
                _app.Delete(ids);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        public InspectPlansController(InspectPlanApp app)
        {
            _app = app;
        }

        /// <summary>
        /// 根据部门id获取区域、标准、设备列表
        /// </summary>
        [HttpGet]
        public Response GetContentItemsById(string orgId)
        {
            var result = new Response<InspectContentList>();
            try
            {
                result.Result = _app.GetContentItemsById(orgId);

            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }

            return result;
        }

        [HttpPost]
        public Response ShiftAddOrUpdate(dynamic obj) //[FromBody]List<InspectShift> shifts
        {
            var result = new Response();
            try
            {
                string type = obj.Type.ToString();
                var shifts = Newtonsoft.Json.JsonConvert.DeserializeObject<List<InspectShift>>(Convert.ToString(obj.Shifts));
                _app.ShiftAddOrUpdate(shifts, type);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpGet]
        public Response GetShiftsForPlan(string id)
        {
            var result = new Response<List<InspectShift>>();

            try
            {
                result.Result = _app.GetInspectShifts(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpGet]
        public Response GetPersonsOption(string userIds)
        {
            var result = new Response<List<User>>();
            try
            {
                result.Result = _app.GetUsers(userIds);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpPost]
        public Response ChangeStatus(InspectPlan inspectPlan)
        {
            var result = new Response();
            _app.ChangeStatus(inspectPlan);
            return result;
        }

        [HttpGet]
        public Response GetInspectPersons(string partId, string search)
        {
            var result = new Response<List<User>>();
            try
            {
                result.Result = _app.GetInspectPersons(partId, search);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 排班
        /// </summary>
        [HttpPost]
        public Response Scheduling(dynamic obj)
        {
            var result = new Response();
            try
            {
                string planId = obj.planId.ToString();
                DateTime startDate = Convert.ToDateTime(obj.startDate);
                DateTime endDate = Convert.ToDateTime(obj.endDate);
                _app.Scheduling(planId, startDate, endDate);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpGet]
        public Response GetScheduList(string planId)
        {
            var result = new Response<List<ScheduleList>>();
            try
            {
                result.Result = _app.GetScheduList(planId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 通过名称查询计划
        /// </summary>
        [HttpGet]
        public Response SearchPlan(string name)
        {
            var result = new Response<List<InspectPlan>>();
            try
            {
                result.Result = _app.GetPlanForName(name);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 轮班模板导入
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public Response<InspectShiftTeamTimeResp> UploadShiftExcel(IFormFile file, [FromForm]string planId)
        {
            var result = new Response<InspectShiftTeamTimeResp>();
            try
            {
                _app.UploadShiftExcel(file, planId);
                result.Result = _app.LoadTeamTime(planId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        [HttpGet]
        public Response<InspectShiftTeamTimeResp> LoadShiftData(string planId)
        {
            var result = new Response<InspectShiftTeamTimeResp>();
            try
            {
                result.Result = _app.LoadTeamTime(planId);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }

        /// <summary>
        /// 获取轮班计划组
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<List<InspectPlan>> GetShiftPlans()
        {
            var result = new Response<List<InspectPlan>>();
            try
            {
                result.Result = _app.GetShiftPlans();
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.InnerException?.Message ?? ex.Message;
            }
            return result;
        }
    }
}
